package com.cxj.musicplayer.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cxj.musicplayer.MainActivity;
import com.cxj.musicplayer.R;
import com.cxj.musicplayer.component.RoundImageView;
import com.cxj.musicplayer.data.DataManager;
import com.cxj.musicplayer.musicsource.network.DownloadFrom163;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;

public class PlayListAdapter extends BaseAdapter {

    private String mData;
    private Context mContext;
    private String[] mId;
    private String[] mTitle;
    boolean ff = false;
    private String[] mPic;
    static int count = 0;
    private HashMap<Integer, View> viewMap = new HashMap<>();

    public PlayListAdapter(Context context, String data){
        mContext = context;
        Log.i(getClass().getName() + ":data", data);
        String[] datas = data.split("♫");
        mId = datas[0].split("☆");
        mTitle = datas[1].split("☆");
        mPic = datas[2].split("☆");
        DataManager.array_data.put("play_list_title", mTitle);
        DataManager.array_data.put("play_list_id", mId);
        DataManager.array_data.put("play_list_pic", mPic);
    }

    @Override
    public int getCount() {
        return mTitle == null ? 0 : mTitle.length;
    }

    @Override
    public Object getItem(int i) {
        return mTitle == null ? null : mTitle[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder;


        if(!viewMap.containsKey(i) || viewMap.get(i) == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.play_list, null);
            viewHolder = new ViewHolder();
            viewHolder.list_name = view.findViewById(R.id.list_name);
            viewHolder.list_pic = view.findViewById(R.id.list_pic);
            view.setTag(viewHolder);
            viewMap.put(i, view);
        } else {
            view = viewMap.get(i);
            viewHolder = (ViewHolder) view.getTag();
        }

        if(viewMap.size() > 20){
            synchronized (view) {
                for(int j = 1; j < ((MainActivity)mContext).gridView().getFirstVisiblePosition() - 3; j ++){
                    viewMap.remove(j);
                }
                for(int j = ((MainActivity)mContext).gridView().getLastVisiblePosition() + 3;j < getCount();j ++){
                    viewMap.remove(j);
                }
            }
        }

        if(viewHolder.list_name.getText().equals("init name")) {
            String t_title = mTitle[i].length() > 10 ? mTitle[i].substring(0, 10) + "..." : mTitle[i];
            viewHolder.list_name.setText(t_title);
        }
        String path = mContext.getApplicationContext().getFilesDir().getAbsolutePath() + "/pic/";
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        final String pic_name = path + mId[i].substring(mId[i].lastIndexOf('/') + 1) + ".jpg";
        File pic = new File(pic_name);

        if (pic.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(pic_name);
                Bitmap bitmap = BitmapFactory.decodeStream(fis);
                viewHolder.list_pic.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(null != fis) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } else {
            final String pic_url = mPic[i];
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.i(getClass().getName() + ":pic_url", pic_url);
                    DownloadFrom163.download_pic(pic_url, pic_name);
                }
            }).start();

        }


        return view;
    }

    static class ViewHolder{
        TextView list_name;
        ImageView list_pic;
    }


}
